/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (C) 2014 RayBow and/or its affiliates. All rights reserved.
 */
package top.swimmer.core.mybatis.convert;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import top.swimmer.core.session.UserType;
import top.swimmer.tools.enums.EnumUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * UserTypeHandler.
 */
public class UserTypeHandler extends BaseTypeHandler<UserType> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UserType parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getId());
    }

    @Override
    public UserType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int i = rs.getInt(columnName);
        if (rs.wasNull()) {
            return null;
        } else {
            try {
                return EnumUtils.resolve(i, UserType.class);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Cannot convert " + i + " to UserType by id value.", ex);
            }
        }
    }

    @Override
    public UserType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        int i = rs.getInt(columnIndex);
        if (rs.wasNull()) {
            return null;
        } else {
            try {
                return EnumUtils.resolve(i, UserType.class);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Cannot convert " + i + " to UserType by id value.", ex);
            }
        }
    }

    @Override
    public UserType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        int i = cs.getInt(columnIndex);
        if (cs.wasNull()) {
            return null;
        } else {
            try {
                return EnumUtils.resolve(i, UserType.class);
            } catch (Exception ex) {
                throw new IllegalArgumentException("Cannot convert " + i + " to UserType by id value.", ex);
            }
        }
    }
}
